package com.sardine.base.esm.session;

import com.sardine.base.esm.binding.EsmMapperRegistry;
import com.sardine.base.esm.executor.Esm212EsmExecutor;
import com.sardine.base.esm.executor.EsmExecutor;
import com.sardine.base.esm.mapping.EsmEnvironment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

/**
 * 上海京东到家元信信息技术有限公司<br>
 * Created with IntelliJ IDEA.
 * Date:2017/5/22
 * Time:11:02
 * esm 的配置对象
 *
 * @author daifeng<br>
 *         daifeng@imdada.cn
 * @since JDK 1.7 <br>
 * Copyright (c) 2017 imdada System Incorporated All Rights Reserved.
 */
public class Configuration {
    private static final Logger logger = LoggerFactory.getLogger(Configuration.class);
    protected final Set<String> loadedResources = new HashSet<String>();
    protected EsmEnvironment esmEnvironment;
    private EsmExecutorType defaultEsmExecutorType = EsmExecutorType.ESM212;
    protected EsmMapperRegistry mapperRegistry = new EsmMapperRegistry(this);

    public EsmEnvironment getEsmEnvironment() {
        return esmEnvironment;
    }

    public void setEsmEnvironment(EsmEnvironment esmEnvironment) {
        this.esmEnvironment = esmEnvironment;
    }

    public <T> boolean hasMapper(Class<?> type) {
        return mapperRegistry.hasMapper(type);
    }

    public <T> void addMapper(Class<T> type) {
        //添加类的映射
        mapperRegistry.addMapper(type);
    }

    public EsmExecutorType getDefaultEsmExecutorType() {
        return defaultEsmExecutorType;
    }

    public EsmExecutor newExecutor(EsmExecutorType esmExecutorType) {
        esmExecutorType = esmExecutorType == null ? defaultEsmExecutorType : esmExecutorType;
        esmExecutorType = esmExecutorType == null ? EsmExecutorType.ESM212 : esmExecutorType;
        EsmExecutor executor = null;
        if (EsmExecutorType.ESM212 == esmExecutorType) {
            executor = new Esm212EsmExecutor(this);
        }
        return executor;
    }

    public <T> T getMapper(Class<T> type, EsmSession sqlSession) {
        return mapperRegistry.getMapper(type, sqlSession);
    }

    public boolean isResourceLoaded(String resource) {
        return loadedResources.contains(resource);
    }

    public void addLoadedResource(String resource) {
        loadedResources.add(resource);
    }
}
